package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.domain.SysArea;
import com.ruoyi.system.service.ISysAreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Family;
import com.ruoyi.system.service.IFamilyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 家庭档案管理Controller
 *
 * @author moweiyu
 * @date 2021-06-09
 */
@Controller
@RequestMapping("/system/family")
public class FamilyController extends BaseController
{
    private String prefix = "system/family";

    @Autowired
    private IFamilyService familyService;
    @Autowired
    private ISysAreaService sysAreaService;

    @RequiresPermissions("system:family:view")
    @GetMapping()
    public String family()
    {
        return prefix + "/family";
    }

    /**
     * 查询家庭档案管理列表
     */
    @RequiresPermissions("system:family:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Family family)
    {
        startPage();
        List<Family> list = familyService.selectFamilyList(family);
        return getDataTable(list);
    }

    /**
     * 导出家庭档案管理列表
     */
    @RequiresPermissions("system:family:export")
    @Log(title = "家庭档案管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Family family)
    {
        List<Family> list = familyService.selectFamilyList(family);
        ExcelUtil<Family> util = new ExcelUtil<Family>(Family.class);
        return util.exportExcel(list, "家庭档案管理数据");
    }

    /**
     * 新增家庭档案管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存家庭档案管理
     */
    @RequiresPermissions("system:family:add")
    @Log(title = "家庭档案管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Family family)
    {
        System.out.println(family.toString());
        SysArea area = new SysArea();
        area.setAreaId(family.getGroupCode());
        /*设置村编号*/
        List<SysArea> areaList = sysAreaService.selectSysAreaList(area);
        family.setCountysideCode(areaList.get(0).getFatherId());
        area.setAreaId(areaList.get(0).getFatherId());
        /*设置镇编号*/
        areaList = sysAreaService.selectSysAreaList(area);
        family.setTownCode(areaList.get(0).getFatherId());
        area.setAreaId(areaList.get(0).getFatherId());
        /*设置县编号*/
        areaList = sysAreaService.selectSysAreaList(area);
        family.setCountyCode(areaList.get(0).getFatherId());
        return toAjax(familyService.insertFamily(family));
    }

    /**
     * 修改家庭档案管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Family family = familyService.selectFamilyById(id);
        mmap.put("family", family);
        return prefix + "/edit";
    }

    /**
     * 修改保存家庭档案管理
     */
    @RequiresPermissions("system:family:edit")
    @Log(title = "家庭档案管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Family family)
    {
        SysArea area = new SysArea();
        area.setAreaId(family.getGroupCode());
        /*设置村编号*/
        List<SysArea> areaList = sysAreaService.selectSysAreaList(area);
        family.setCountysideCode(areaList.get(0).getFatherId());
        area.setAreaId(areaList.get(0).getFatherId());
        /*设置镇编号*/
        areaList = sysAreaService.selectSysAreaList(area);
        family.setTownCode(areaList.get(0).getFatherId());
        area.setAreaId(areaList.get(0).getFatherId());
        /*设置县编号*/
        areaList = sysAreaService.selectSysAreaList(area);
        family.setCountyCode(areaList.get(0).getFatherId());
        return toAjax(familyService.updateFamily(family));
    }

    /**
     * 删除家庭档案管理
     */
    @RequiresPermissions("system:family:remove")
    @Log(title = "家庭档案管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(familyService.deleteFamilyByIds(ids));
    }
}
